//
//  SYStatusRetweetView.swift
//  SYWeibo
//
//  Created by Eric on 16/7/24.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
import SnapKit
class SYStatusRetweetView: UIView {
    //记录当前原创微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    //供外界调用
    var statusViewModel: SYStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
            /*
            - 卸载转发微博底部约束
            - 如果配图count > 0
            - 01 给配图赋值
            - 02 更改约束
            - 03 显示配图
            - 反之
            - 01 更改约束
            - 02 隐藏配图
            */
            
            retweetViewBottomConstraint?.uninstall()//卸载约束
            //代表有配图
            if statusViewModel?.status?.retweeted_status?.pic_urls?.count > 0{
                //赋值
                pictureView.pictuerInfo = statusViewModel?.status?.retweeted_status?.pic_urls
                //更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
                })
                //显示配图
                pictureView.hidden = false
            }else{
                //代表没配图
                //更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(statusCellMargin).constraint
                })
                //显示视图
                pictureView.hidden = true
                
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
    backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(statusCellMargin).constraint
        }
        //添加pictureView的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo((SCREENW - (cellWH*3 + cellMargin*2))/2)
            
        }
        
        //当前的View
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
        }
    }
    // MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 20)
    private lazy var pictureView: SYStatusPictureView = SYStatusPictureView()
}
